<?php
/**
 * Created by JetBrains PhpStorm.
 * User: dvynograd
 * Date: 22.01.13
 * Time: 15:22
 * To change this template use File | Settings | File Templates.
 */

namespace Vozminoski\StoreBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
use Vozminoski\StoreBundle\Entity\Product;
use Vozminoski\StoreBundle\Entity\Image;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;


class LoadProductData extends AbstractFixture implements OrderedFixtureInterface
{
    /**
     * {@inheritDoc}
     */
    public function load(ObjectManager $manager)
    {

        $sizes = $manager->getRepository('VozminoskiStoreBundle:Size')->findAll();
        $colors = $manager->getRepository('VozminoskiStoreBundle:Color')->findAll();

        foreach (array('man', 'women', 'children') as $categoryName ) {
            $orderNumber = 0;
            for ($i=0; $i < 6; $i++) {
                $category = $this->getReference($categoryName . '-category');
                $product = new Product();
                $product->setName('Носки ' . $category->getName());
                $product->setPrice(100);
                $product->setDescription('Хлопковая пряжа является самым распространенным на сегодняшний день материалом '
                    . 'растительного происхождения. Носки из хлопка всем хорошо знакомы. Они устойчивы к истиранию, '
                    . 'хорошо впитывают влагу, пропускают воздух и не вызывают аллергии.');

                $product->setComposition(serialize(array('Хлопок' => 80, 'Па' => 15, 'Лайкра' => 5)));
                $product->setOrder(++$orderNumber);
                $product->setCategory($category);
                $product->setIsActive(true);
                $product->setDiscount(serialize(array(
                    array('quantity'=> 5, 'type' => 0, 'value' => 5),
                    array('quantity'=> 15, 'type' => 0, 'value' => 10),
                    array('quantity'=> 30, 'type' => 0, 'value' => 15),
                )));
                $product->setPath($categoryName . '.png');
                $this->_addSizes($product, $sizes);
                $this->_addColors($product, $colors);
                $manager->persist($product);
                $this->addImages($product, $manager);

            }
        }
        $manager->flush();
    }

    /**
     * Add images to product
     *
     * @param \Vozminoski\StoreBundle\Entity\Product $product
     * @param \Doctrine\Common\Persistence\ObjectManager $manager
     */
    protected function addImages(Product $product, ObjectManager $manager)
    {
        foreach (array('test.jpg', 'test1.jpg', 'test2.jpg', 'test3.jpg') as $imageName) {
            $image = new Image();
            $image->setPath($imageName);
            $image->setProduct($product);
            $manager->persist($image);
            $manager->flush();
        }
    }


    /**
     * Add all sizes to Product
     * @param \Vozminoski\StoreBundle\Entity\Product $product
     * @param array $sizes
     */
    protected function _addSizes(Product $product, $sizes)
    {
        foreach ($sizes as $size) {
            $product->addSize($size);
        }
    }

    /**
     * Add all colors to Product
     * @param \Vozminoski\StoreBundle\Entity\Product $product
     * @param array $colors
     */
    protected function _addColors(Product $product, $colors)
    {
        foreach ($colors as $color) {
            $product->addColor($color);
        }
    }

    /**
     * {@inheritDoc}
     */
    public function getOrder()
    {
        return 2; // the order in which fixtures will be loaded
    }
}